
// Table E.1 and E.2

/* =============================================================================
   Step 1: load data 
   ===========================================================================*/
   
use  "${dataout}DrivingDataset.dta", clear

/* Make auxiliary variables */
gen cars_alt = cars
replace cars_alt = 3 if cars >= 3

gen km_icev = km_dag
replace km_icev = . if electric == 1

gen km_bev = km_dag 
replace km_bev = . if electric == 0

/* average time and distance for both household members */
gen dist = (dist1 + dist2)/2
gen time = (time1 + time2)/2

/* labeling variables for estout */
label var km_dag "Driving (km per day per car)"
label var km_icev "Driving, ICEV (km per day per car)"
label var km_bev "Driving, BEV (km per day per car)"
label var days_eu "Length of driving period (days)"
label var diesel "Diesel car (yes = 1)"
label var age_car "Car age (years)"

label var days_hh "Days owned by modal household"
label var cars "Cars owned by modal household"
label var dist "Distance to work (km)"
label var time "Time to work (min)"
label var toll_fam_mean "Toll to work (NOK)"
label var ptl_fam_km_mean "Bus lane to work (km)"

label var year_2011 "2011"
label var year_2012 "2012"
label var year_2013 "2013"
label var year_2014 "2014"
label var year_2015 "2015"
label var year_2016 "2016"
label var year_2017 "2017"

/* =============================================================================
   Table E.1
==============================================================================*/
* Selecting variables
local varlist1 km_dag days_eu diesel age_car // Panel A: Car-specific
local varlist2 days_hh cars dist time toll_fam_mean ptl_fam_km_mean // Panel B: Car-owner-specific
local varlist3 /*year_2011 year_2012*/ year_2013 year_2014 year_2015 year_2016 year_2017 // Panel C: Years of driving period

* Storing summary statistics
eststo clear
* All cars
eststo g1: estpost summarize `varlist1' `varlist2' `varlist3' ///
	if year_2005 == 0 ///
	& year_2006 == 0 ///
	& year_2007 == 0 ///
	& year_2008 == 0 ///
	& year_2009 == 0 ///
	& year_2010 == 0 ///
	& year_2011 == 0 ///
	& year_2012 == 0 ///
	& weight > 0.5 ///
	& lnkm > 0

* ICEVs	
eststo g2: estpost summarize `varlist1' `varlist2' `varlist3' ///
	if year_2005 == 0 ///
	& year_2006 == 0 ///
	& year_2007 == 0 ///
	& year_2008 == 0 ///
	& year_2009 == 0 ///
	& year_2010 == 0 ///
	& year_2011 == 0 ///
	& year_2012 == 0 ///
	& weight > 0.5 ///
	& lnkm > 0 ///
	& electric == 0

* BEVs	
eststo g3: estpost summarize `varlist1' `varlist2' `varlist3' ///
	if year_2005 == 0       ///
	& year_2006 == 0       ///
	& year_2007 == 0      ///
	& year_2008 == 0     ///
	& year_2009 == 0    ///
	& year_2010 == 0   ///
	& year_2011 == 0  ///
	& year_2012 == 0 ///
	& weight > 0.5  ///
	& lnkm > 0     ///
	& electric == 1

* Exporting table	
esttab g1 g2 g3 using "${tables}TableE1.tex" , replace ///
	mgroups("All cars" "ICEVs" "BEVs", pattern(1  1  1 ) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat (\cmidrule(lr){@span}) ) ///
	refcat(km_dag "\textbf{Panel A: Car-specific}" days_hh "\textbf{Panel B: Car-owner-specific}" year_2013 "\textbf{Panel C: Years of driving period}", nolabel) ///
	cells("mean(fmt(2 0 3 2 0 2 2 2 2 3 3 3 3 3 3 3 3)) sd(par fmt(2 0 3 2 0 2 2 2 2 3 3 3 3 3 3 3 3))") ///
	label booktabs nonumbers nonotes	
		
/* =============================================================================
	Table E2
============================================================================= */

eststo clear
* Storing summary statistics
eststo E2a: estpost summarize ///
	km_icev age_car cars dist time toll_fam_mean ptl_fam_km_mean /// variable list
	if year_2005 == 0   /// Same selection criteria as in "Table 5.do"
	& year_2006 == 0    ///
	& year_2007 == 0    ///
	& year_2008 == 0    ///
	& year_2009 == 0    ///
	& year_2010 == 0    ///
	& year_2011 == 0    ///
	& year_2012 == 0    ///
	& weight > 0.5      ///
	& lnkm > 0          ///
	& dist1 != . & dist2 != . 	/// Couples only
	& electric == 0 			/// ICEVs only
	& cars_alt < 1.5 			//  one-car households
* Note: lt 1.5 means in average less than 1.5 cars. I.e. the household did not 
* own two cars for more than half of the ownership period
	
eststo E2b: estpost summarize ///
	km_bev age_car cars dist time toll_fam_mean ptl_fam_km_mean ///
	if year_2005 == 0   ///
	& year_2006 == 0    ///
	& year_2007 == 0    ///
	& year_2008 == 0    ///
	& year_2009 == 0    ///
	& year_2010 == 0    ///
	& year_2011 == 0    ///
	& year_2012 == 0    ///
	& weight > 0.5      ///
	& lnkm > 0          ///
	& dist1 != . & dist2 != . 	/// Couples only
	& electric == 1 			/// BEVs only
	& cars_alt < 1.5			// one-car households
	
eststo E2c: estpost summarize ///
	km_icev age_car cars dist time toll_fam_mean ptl_fam_km_mean ///
	if year_2005 == 0   ///
	& year_2006 == 0    ///
	& year_2007 == 0    ///
	& year_2008 == 0    ///
	& year_2009 == 0    ///
	& year_2010 == 0    ///
	& year_2011 == 0    ///
	& year_2012 == 0    ///
	& weight > 0.5      ///
	& lnkm > 0          ///
	& dist1 != . & dist2 != . 	/// Couples only
	& electric == 0 			/// First car ICEV
	& electric_secondary < 0.5 	/// Second car ICEV
	& cars_alt >= 1.5 & cars_alt != . // Two-car households
	
eststo E2d: estpost summarize ///
	km_icev km_bev age_car cars dist time toll_fam_mean ptl_fam_km_mean ///
	if year_2005 == 0   ///
	& year_2006 == 0    ///
	& year_2007 == 0    ///
	& year_2008 == 0    ///
	& year_2009 == 0    ///
	& year_2010 == 0    ///
	& year_2011 == 0    ///
	& year_2012 == 0    ///
	& weight > 0.5      ///
	& lnkm > 0          ///
	& dist1 != . & dist2 != . 						/// Couples only
	& ( electric == 1 | electric_secondary >= 0.5 ) /// First and/or second car BEV
	& cars_alt >= 1.5 & cars_alt != . 				// Two-car household	

esttab E2*, ///
	mgroups("One car" "Two+ cars", pattern(1 0 1 0)) ///
	cells("mean(fmt(2))") ///
	order(km_icev km_bev age_car cars dist time toll_fam_mean ptl_fam_km_mean) ///
	label nonumbers nonotes	mtitle("ICEV only" "BEV only" "ICEV only" "Incl. BEV")	collabels(none)

* Exporting table	
esttab E2* using "${tables}TableE2.tex" , replace ///
	mgroups("One car" "Two+ cars", pattern(1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat (\cmidrule(lr){@span}) ) ///
	mtitle("ICEV only" "BEV only" "ICEV only" "Incl. BEV") ///
	cells("mean(fmt(2))") ///
	order(km_icev km_bev age_car cars dist time toll_fam_mean ptl_fam_km_mean) ///
	label booktabs nonumbers nonotes collabels(none)	
	